package com.netflix.conductor.server.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Response object to return a list of succeeded entities and a map of failed ones, including error message, for the bulk request.
 */
public class BulkResponse {

    /**
     * Key - entityId
     * Value - error message processing this entity
     */
    private final Map<String, String> bulkErrorResults;
    private final List<String> bulkSuccessfulResults;
    private final String message = "Bulk Request has been processed.";

    public BulkResponse() {
        this.bulkSuccessfulResults = new ArrayList<>();
        this.bulkErrorResults = new HashMap<>();
    }

    public List<String> getBulkSuccessfulResults() {
        return bulkSuccessfulResults;
    }

    public Map<String, String> getBulkErrorResults() {
        return bulkErrorResults;
    }

    public void appendSuccessResponse(String id) {
        bulkSuccessfulResults.add(id);
    }

    public void appendFailedResponse(String id, String errorMessage) {
        bulkErrorResults.put(id, errorMessage);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BulkResponse)) return false;
        BulkResponse that = (BulkResponse) o;
        return Objects.equals(bulkSuccessfulResults, that.bulkSuccessfulResults) &&
                Objects.equals(bulkErrorResults, that.bulkErrorResults) &&
                Objects.equals(message, that.message);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bulkSuccessfulResults, bulkErrorResults, message);
    }

    @Override
    public String toString() {
        return "BulkResponse{" +
                "bulkSuccessfulResults=" + bulkSuccessfulResults +
                ", bulkErrorResults=" + bulkErrorResults +
                ", message='" + message + '\'' +
                '}';
    }
}
